package net.googlecode.demenkov.dao.impl;

import net.googlecode.demenkov.dao.GroupDAO;
import net.googlecode.demenkov.domains.Group;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.List;

/**
 * DAO class for actions with groups
 *
 * @author Demenkov Yura
 */
@Repository
public class GroupDAOImpl extends GenericDAOImpl<Group, Integer> implements GroupDAO {
    public Group findGroupByGroupNameAndFacultyNameAndUniversityName(String groupName, String factultyName, String universityName) {
        Query query = entityManager.createQuery("from Group where groupName=:groupname " +
                "and faculty.facultyName=:facultyname and faculty.university.universityName=:universityname");
        query.setParameter("groupname", groupName);
        query.setParameter("facultyname", factultyName);
        query.setParameter("universityname", universityName);
        if (!query.getResultList().isEmpty()) {
            return (Group) query.getResultList().get(0);
        } else {
            return null;
        }
    }

    public List<Group> findAllGroupsByFacultyNameAndUniversityName(String facultyName, String universityName) {
        Query query = entityManager.createQuery("from Group where faculty.facultyName=:facultyname " +
                "and faculty.university.universityName=:universityname");
        query.setParameter("facultyname", facultyName);
        query.setParameter("universityname", universityName);
        return query.getResultList();
    }
}
